<?php

namespace Admin\Controller;
use Think\Controller;

use Admin\Model\UserModel;
use Admin\Model\AuthModel;
/**
 * Description of CommonController
 *
 * @author wangnaixin
 * @todo 后台父类 处理 登录用户可以看到的菜单 初始化用户数据
 */


class AdminController extends Controller {
    //put your code here

    public $userId;
    public $userInfo;
    public $userAuths;

    function __construct() {
        parent::__construct();
        $this->checkUser();
        if($this->checkAuth(CONTROLLER_NAME,ACTION_NAME) === false){
            $this->error("无授权!");
        }
        $this->assign("controllerName",CONTROLLER_NAME);
        $this->assign("actionName",ACTION_NAME);
        $dayWeek = date('Y\年m\月d\日',time());
        $weeks = [
            '1'=>"一",
            '2'=>'二',
            '3'=>'三',
            '4'=>'四',
            '5'=>'五',
            '6'=>'六',
            '7'=>'七'
        ];
        $dayWeek .= " 星期".$weeks[date('N',time())];
        $this->assign("dayWeek",$dayWeek);
        $this->assign('imageServerUrl',C('IMG_PATH'));
        $this->assign('serverName',SERVER_NAME.__ROOT__);
        $this->setSysPageTitle();
    }

    /**
     * @todo 判断用户是否登录 未登录跳转登录页面
     */
    private function checkUser(){
        $userInfo = session('userInfo');
        if(empty($userInfo)){
            redirect(U('Admin/Login/login'));
        }
        $this->userId = session('userId');
        $this->userInfo = $userInfo;
        $this->userAuths = $userInfo['authList'];
        $this->assign("menuList",$userInfo['menuList']);
    }

    /**
     * @todo 判断用户是否有权限操作
     */
    private function checkAuth($c ,$a){
        if (in_array($c.'/'.$a,C('AUTH_WHITELIST'))) {
            return true;
        }
        if($this->userInfo['role']['super'] == 1){
            return true;
        }
        if(!in_array($a, $this->userAuths[$c])){
            return false;
        }else{
            return true;
        }
    }
    /**
     * @todo 查询操作
     */
    protected function setForm(){
        $formData = I('post.');
        $this->assign($formData);
    }

    protected function clearUserSession(){
        $user = new UserModel();
        $auth = new AuthModel();

        $data = $user->doLoginById($this->userId);
        if($data !== false){
            //检查用户权限
            if($data['role']['back_login']){
                //处理菜单
                if($data['role']['super'] == 1){
                    $data['menuList'] = menuInit($auth->where("isMenu=1")->select());
                }else{
                    $data['menuList'] = menuInit($data['auths']);
                }
                $data['authList'] = authInit($data['auths']);
                unset($data['auths']);
                session("userInfo",$data);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 设置分页数据
     * @param $data
     */
    protected function setPageData($data) {
        $this->assign('listData',$data['list']);
        $this->assign('page',$data['page']);
        $this->assign('nowPage',$data['nowPage']);
        $this->assign('pageSize',$data['pageSize']);
    }

    protected function setSysPageTitle() {
        $sysPageTitle = C('SYS_PAGE_TITLE');
        $this->assign('sysPageTitle',$sysPageTitle);
    }
}
